<template>
  <div class="app-container">
    <div>
<!--      <div style="margin-top: 15px;margin-bottom: 15px;">-->
<!--        <el-button type="success" @click="showCreate">添加咨询高校</el-button>-->
<!--      </div>-->
      <div style="margin-top: 15px;margin-bottom: 15px;">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="高校名称" v-model="search"></el-input>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </el-col>
        </el-row>

      </div>
      <div>
        <el-table border :data="table.data" v-loading="table.loading">
          <el-table-column label="咨询高校" prop="academy_name"></el-table-column>
<!--          <el-table-column label="省份" prop="province.label"></el-table-column>-->
          <el-table-column label="是否显示">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.is_advisory_show===1" type="success">显示</el-tag>
              <el-tag v-if="scope.row.is_advisory_show===2" type="danger">不显示</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="advisory_sort"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="showEdit(scope.row)">编辑</el-button>
<!--              <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-if="table.total > 0" style="margin-top: 15px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="table.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="table.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="table.total">
        </el-pagination>
      </div>
    </div>
    <div>
      <el-dialog :visible.sync="createDialog.visible" :title="createDialog.title" @close="handleClose">
        <el-form :model="form" :rules="createDialog.rules" ref="createForm">
<!--          <el-form-item label="咨询高校" prop="academy_code">-->
<!--            <el-select v-model="form.academy_code" style="width: 100%" filterable remote placeholder="请输入关键词"-->
<!--                       :remote-method="getAcademyList"-->
<!--                       :loading="createDialog.loading.academy">-->
<!--              <el-option-->
<!--                v-for="item in createDialog.academies"-->
<!--                :key="item.value"-->
<!--                :label="item.label"-->
<!--                :value="item.value">-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
          <el-form-item label="省份操作" prop="type">
            <el-button type="primary" @click="handleProvince([])">清空</el-button>
            <el-button type="primary" @click="handleProvince(createDialog.provinces.map(province=>province.value))">全国</el-button>
          </el-form-item>
          <el-form-item label="省份" prop="province_id">
            <el-select v-model="form.province_id" style="width: 100%" filterable placeholder="请输入关键词" :loading="createDialog.loading.province" multiple clearable="clearable">
              <el-option
                v-for="item in createDialog.provinces"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" prop="advisory_sort">
            <el-input v-model.number="form.advisory_sort"></el-input>
          </el-form-item>
          <el-form-item label="是否显示" prop="is_advisory_show">
            <el-switch
              v-model="form.is_advisory_show"
              active-color="#13ce66"
              :active-value="1"
              active-text="是"
              inactive-text="否"
              :inactive-value="2"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="handleCreate" :disabled="createDialog.disabled.saveButton"  v-if="createDialog.type==='create'">保存</el-button>
            <el-button type="success" @click="handleCreate" :disabled="createDialog.disabled.saveButton" v-if="createDialog.type==='edit'">保存</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import {
    createAdvisory, deleteAdvisory,
    getAcademyList,
    getAdvisoryList,
    getProvinceList,
    updateAdvisory
  } from '../../api/advisoryAcademy'

  const form = () => {
    return {
      province_id: [],
      academy_code: '',
      advisory_sort: 10,
      type: 1,
      is_advisory_show: 2
    }
  }
  const rules = () => {
    return {
      province_id: [
        { required: true, message: '请选择省份', trigger: 'blur' }
      ],
      academy_code: [
        { required: true, message: '请选择咨询高校', trigger: 'blur' }
      ],
      sort: [
        { required: true, message: '请填写排序', trigger: 'blur' },
        { type: 'number', message: '排序必须为数字' }
      ],
      is_show: [
        { required: true, message: '请选择是否显示', trigger: 'blur' }
      ]
    }
  }
  export default {
    data() {
      return {
        table: {
          data: [],
          page: 1,
          total: 0,
          loading: false,
          limit: 10
        },
        search: '',
        form: form(),
        createDialog: {
          id: '',
          type: '',
          visible: false,
          title: '',
          provinces: [],
          academies: [],
          rules: rules(),
          loading: {
            academy: false,
            province: false
          },
          disabled: {
            saveButton: false
          }
        }
      }
    },
    mounted() {
      this.init()
    },
    methods: {
      handleSearch() {
        this.table.page = 1
        this.getList()
      },
      handleProvince(provinces) {
        console.log(provinces)
        this.form.province_id = provinces
      },
      handleClose() {
        this.$refs['createForm'].resetFields()
        this.resetFields()
      },
      handleEdit() {
        this.$refs['createForm'].validate((valid) => {
          if (valid) {
            this.createDialog.disabled = true
            updateAdvisory(this.form, this.createDialog.id).then(res => {
              this.resetFields()
              this.createDialog.visible = false
              this.getList()
            }).finally(() => {
              this.createDialog.disabled = false
            })
          } else {
            return false
          }
        })
      },
      showEdit(row) {
        this.createDialog.id = row.id
        this.createDialog.type = 'edit'
        this.createDialog.title = '编辑咨询高校-' + row.academy_name
        this.form.is_advisory_show = row.is_advisory_show
        this.form.province_id = row.advisories.map(p => {
          return p.province_id
        })
        // const loading = this.$loading({
        //   lock: true,
        //   text: '加载中...',
        //   spinner: 'el-icon-loading',
        //   background: 'rgba(0, 0, 0, 0.7)'
        // })
        // this.getAcademyList(row.academy_name).then(() => {
        //   this.form.academy_code = row.academy_code
        //   this.createDialog.visible = true
        //   loading.close()
        // })
        this.form.academy_code = row.academy_code
        this.form.advisory_sort = row.advisory_sort
        this.createDialog.visible = true
      },
      handleDelete(row) {
        this.$confirm('您正在进行删除操作, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAdvisory(row.id).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      handleCreate() {
        this.$refs['createForm'].validate((valid) => {
          if (valid) {
            this.createDialog.disabled = true
            createAdvisory(this.form).then(res => {
              this.resetFields()
              this.createDialog.visible = false
              this.getList()
            }).finally(() => {
              this.createDialog.disabled = false
            })
          } else {
            return false
          }
        })
      },
      handleSizeChange(size) {
        this.table = () => {
          this.table.page = 1
          this.table.limit = size
          return this.table
        }
        this.getList()
      },
      handleCurrentChange(page) {
        this.table.page = page
        this.getList()
      },
      getList() {
        this.table.loading = true
        getAdvisoryList({
          limit: this.table.limit,
          page: this.table.page,
          search: this.search
        }).then(res => {
          this.table.data = res.data.data
          this.table.total = res.data.meta.total
        }).finally(() => {
          this.table.loading = false
        })
      },
      showCreate() {
        this.createDialog.type = 'create'
        this.createDialog.title = '添加咨询高校'
        this.createDialog.visible = true
      },
      getAcademyList(query) {
        if (query) {
          this.createDialog.loading.academy = true
          return getAcademyList({
            search: query
          }).then(res => {
            console.log('pr', res)
            this.createDialog.academies = res.data.data
          }).finally(() => {
            this.createDialog.loading.academy = false
          })
        } else {
          this.createDialog.academies = []
          return new Promise()
        }
      },
      init() {
        getProvinceList().then(res => {
          console.log('pr', res)
          this.createDialog.provinces = res.data
        })
        this.getList()
      },
      resetFields() {
        this.form = form()
      }
    }
  }
</script>

<style scoped>

</style>
